const Base = require('./base.js');
const axios = require("axios");
const file = require("fs");
const path = require("path");
module.exports = class extends Base {
  indexAction() {
    return this.display();
  };

  async getCameraDataAction() {
    let param = this.post();
    if (param.action == "getCameraData") {
      const options = await this.model("camera").parseOptions({pk: "mac"});
      let cameras = await this.model("camera").page(param.currentPage, param.pageSize).countSelect(options);
      for (let i = 0; i < cameras.data.length; i++) {
        cameras.data[i].showid = ((param.currentPage - 1) * param.pageSize) + i + 1;
      }
      if (cameras) {
        return this.success(cameras);
      }
    }
  }

  async editCameraAction() {
    let params = this.post();
    await this.model("camera").where({ id: params.camera.id }).update(params.camera);
    return this.success("摄像头信息编辑成功");
  };

  async addCameraAction() {
    let params = this.post();
    // 调用 HttpService 中的 requestAction 方法
    try {
      //params.zlUrl = "192.168.2.22/apptest/" +  
      let insertId = await this.model("camera").add(params.camera);
      // params.camera.id = insertId;
      // params.camera.zlurl = `192.168.2.23/app/${insertId}`
      // await this.model("camera").update(params.camera);
      // const zlParams = {
      //   secret: 'fK4ry3ik8RZA9BAFFy874QcPG5jF4XBV',
      //   vhost: '192.168.152.129',
      //   app: params.camera.name,
      //   stream: insertId,
      //   url: params.camera.url,
      //   timeout_sec: '15',
      // };
      // const data = await this.service("http").post("/index/api/addStreamProxy", zlParams);
      // console.log(data.data);
      // await this.model("camera").where({id: insertId}).update({key: data.data.data.key})
      
      return this.success("新增摄像头成功");
    } catch (error) {
      // 处理错误
      console.error(error + '3321');
      // this.fail('Failed to fetch data'); // 返回错误给客户端
    }

  }
  async removeCameraAction() {
    let params = this.post();
    if (params.action == "remove") {
      let id = await this.model("camera").where({ id: params.camera.id }).delete();
      if (id) {
        return this.success("ok")
      }
    }
  };
  async GetSettingAction(){
    let param = this.post();
    if (param.action == "getCameraData") {
      let content = await file.readFileSync(path.join(think.ROOT_PATH, 'runtime/videoSetting/setting.json'));
      if(content.toString() != "" && param.split == false){
        let data = JSON.parse(content.toString());
        data.isSetting = true;
        let cameras = await this.model("camera").page(param.currentPage, param.pageSize).countSelect();
        for (let i = 0; i < cameras.data.length; i++) {
          cameras.data[i].showid = ((param.currentPage - 1) * param.pageSize) + i + 1;
        }
        data.cameras = cameras;
        return this.success(data);
      }else{
        let cameras = await this.model("camera").page(param.currentPage, param.pageSize).countSelect();
        for (let i = 0; i < cameras.data.length; i++) {
          cameras.data[i].showid = ((param.currentPage - 1) * param.pageSize) + i + 1;
        }
        if (cameras) {
          cameras.isSetting = false;
          return this.success(cameras);
        }
      }
      
      
    }
    
  }
  async SaveSettingAction(){
    let params = this.post();
    let str = JSON.stringify(params);
    let res = await file.writeFileSync(path.join(think.ROOT_PATH, 'runtime/videoSetting/setting.json'), str);
    console.log(res);
    return this.success("保存成功");
  }
  async ClearSettingAction(){
    let str = {
      cellCount:4,
      videoArr:[]
    }
    let cameras = await this.model("camera").page(1, 4).countSelect();
    console.log(cameras);
    for(let i = 0; i < 4; i++){
      if(cameras.data[i]){
        str.videoArr.push(cameras.data[i].key + "-" + i);
      }else{
        str.videoArr.push("")
      }
      
    }
    
    await file.writeFileSync(path.join(think.ROOT_PATH, 'runtime/videoSetting/setting.json'), JSON.stringify(str));
    return this.success("重置成功");
  }
};
